package com.zlf.sb.demo.handler;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Slf4j
public class MyInvocationHandler<T> implements InvocationHandler {

    private T mapper;

    public MyInvocationHandler(T mapper) {
        this.mapper = mapper;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("=======jdk代理调用开始=======");
        method.invoke(mapper, args);
        log.info("=======jdk代理调用结束=======");
        return null;
    }

    /**
     * JDK动态代理获取mapper的代理对象
     *
     * @return
     */
    public T getNewProxyInstance() {
        T myproxy = (T) Proxy.newProxyInstance(mapper.getClass().getClassLoader(), mapper.getClass().getInterfaces(), this);
        return myproxy;
    }

}
